package com.medical.controller;

import com.medical.pojo.WeatherDailyResponse;
import com.medical.pojo.WeatherForecastResponse;
import com.medical.pojo.WeatherResponse;
import com.medical.service.WeatherService;
import com.medical.vo.GenericResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/weather")
@Slf4j
public class WeatherController {
    @Autowired
    private WeatherService weatherService;

    @GetMapping("/now")
    public GenericResult<WeatherResponse> getNowWeather(@RequestParam Double lat, @RequestParam Double lon) {
        try {
            WeatherResponse result = weatherService.getNowWeather(lat, lon);
            if (result != null && "200".equals(result.getCode())) {
                return GenericResult.successData(result);
            }
            log.warn("获取天气数据失败: code={}, updateTime={}",
                    result != null ? result.getCode() : "null",
                    result != null ? result.getUpdateTime() : "null");
            return GenericResult.error("获取天气数据失败");
        } catch (Exception e) {
            log.error("获取天气数据异常: lat={}, lon={}", lat, lon, e);
            return GenericResult.error("系统异常：" + e.getMessage());
        }
    }
    @GetMapping("/forecast/24h")
    public GenericResult<WeatherForecastResponse> get24HourForecast(@RequestParam Double lat, @RequestParam Double lon) {
        try {
            WeatherForecastResponse result = weatherService.get24HourForecast(lat, lon);
            if (result != null && "200".equals(result.getCode())) {
                return GenericResult.successData(result);
            }
            return GenericResult.error("获取24小时预报失败");
        } catch (Exception e) {
            log.error("获取24小时预报异常", e);
            return GenericResult.error("系统异常：" + e.getMessage());
        }
    }

    @GetMapping("/forecast/7d")
    public GenericResult<WeatherDailyResponse> get7DayForecast(@RequestParam Double lat, @RequestParam Double lon) {
        try {
            WeatherDailyResponse result = weatherService.get7DayForecast(lat, lon);
            if (result != null && "200".equals(result.getCode())) {
                return GenericResult.successData(result);
            }
            return GenericResult.error("获取7天预报失败");
        } catch (Exception e) {
            log.error("获取7天预报异常", e);
            return GenericResult.error("系统异常：" + e.getMessage());
        }
    }
}
